﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.Common.IP;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class SysIpBlock
    {
        private readonly KeRui.DAL.SysIpBlock dal = new KeRui.DAL.SysIpBlock();

        public int Add(KeRui.Model.SysIpBlock model)
        {
            return this.dal.Add(model);
        }

        public bool CheckName(int ID, string Name, int BlockType, int BlockModule)
        {
            string strWhere = string.Concat(new object[] { "BlockModule=", BlockModule, " and BlockType=", BlockType, " and Name='", Utils.SqlStringFormat(Name), "'" });
            if (ID > 0)
            {
                strWhere = strWhere + "and ID<>" + ID;
            }
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return (common.GetCount("SysIpBlock", strWhere) > 0);
        }

        public List<KeRui.Model.SysIpBlock> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.SysIpBlock> list = new List<KeRui.Model.SysIpBlock>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.SysIpBlock item = new KeRui.Model.SysIpBlock();
                    if (dt.Rows[i]["ID"].ToString() != "")
                    {
                        item.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                    }
                    item.IpStart = long.Parse(dt.Rows[i]["IpStart"].ToString());
                    item.IpEnd = long.Parse(dt.Rows[i]["IpEnd"].ToString());
                    item.Name = dt.Rows[i]["Name"].ToString();
                    if (dt.Rows[i]["BlockType"].ToString() != "")
                    {
                        item.BlockType = int.Parse(dt.Rows[i]["BlockType"].ToString());
                    }
                    if (dt.Rows[i]["BlockModule"].ToString() != "")
                    {
                        item.BlockModule = int.Parse(dt.Rows[i]["BlockModule"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int ID)
        {
            this.dal.Delete(ID);
        }

        public bool Exists(int BlockModule, int BlockType, string IPstr)
        {
            long num = BlockIP.CalcIP(IPstr);
            string strWhere = string.Concat(new object[] { "BlockModule=", BlockModule, " and BlockType=", BlockType, " and (", num, " BETWEEN IpStart and IpEnd)" });
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return (common.GetCount("SysIpBlock", strWhere) > 0);
        }

        public List<KeRui.Model.SysIpBlock> GetList(int BlockModule, int BlockType)
        {
            string strWhere = string.Concat(new object[] { "BlockModule=", BlockModule, " and BlockType=", BlockType });
            string strOrder = "ID desc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("SysIpBlock", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.SysIpBlock> GetListByCache(int BlockModule, int BlockType)
        {
            string cacheKey = string.Concat(new object[] { "SysIpBlockList-", BlockModule, "-", BlockType });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(BlockModule, BlockType);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.SysIpBlock>) cache;
        }

        public KeRui.Model.SysIpBlock GetModel(int ID)
        {
            return this.dal.GetModel(ID);
        }

        public KeRui.Model.SysIpBlock GetModelByCache(int ID)
        {
            string cacheKey = "SysIpBlockModel-" + ID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetModel(ID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.SysIpBlock) cache;
        }

        public void Update(KeRui.Model.SysIpBlock model)
        {
            this.dal.Update(model);
        }
    }
}

